package com.zl.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.system.comm.util.DateUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisBeautyConfigMapper;
import com.system.common.mapper.BisBeautyRecordMapper;
import com.system.common.mapper.BisBusinessOrderMapper;
import com.system.common.mapper.BisCustomUserMapper;
import com.system.common.mapper.BisNurseProjectMapper;
import com.system.common.mapper.BisOrderDepositMapper;
import com.system.common.mapper.BisOrderPayMapper;
import com.system.common.mapper.BisShopCollectMapper;
import com.system.common.mapper.BisShopCustomMapper;
import com.system.common.mapper.BisShopMapper;
import com.system.common.pojo.BisBeautyConfig;
import com.system.common.pojo.BisBeautyConfigExample;
import com.system.common.pojo.BisBeautyRecord;
import com.system.common.pojo.BisBeautyRecordExample;
import com.system.common.pojo.BisBusinessOrder;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisNurseProject;
import com.system.common.pojo.BisOrderDeposit;
import com.system.common.pojo.BisOrderPay;
import com.system.common.pojo.BisShop;
import com.system.common.pojo.BisShopCollect;
import com.system.common.pojo.BisShopCollectExample;
import com.system.common.pojo.BisShopCustom;
import com.system.common.pojo.BisShopCustomExample;
import com.system.common.pojo.BisShopExample;
import com.system.common.pojo.BisShopExample.Criteria;

@Service
public class ZlBisShopServiceImpl implements ZlBisShopService{
    
	
	@Autowired
	private BisShopMapper bisShopMapper;
	@Autowired
	private BisCustomUserMapper bisCustomUserMapper;
	@Autowired
	private BisOrderPayMapper bisOrderPayMapper;
    @Autowired
    private BisOrderDepositMapper bisOrderDeposit;
    @Autowired
    private BisNurseProjectMapper bisNurseProjectMapper;
    @Autowired 
    private BisShopCustomMapper shopCustomMapper;
    @Autowired
    private BisBusinessOrderMapper bisBusinessOrderMapper;
    @Autowired 
    private BisBeautyConfigMapper bisBeautyConfigMapper;
    @Autowired
    private BisBeautyRecordMapper bisBeautyRecordMapper;
    @Autowired
    private BisShopCollectMapper bisShopCollectMapper;
	@Override
	public List<BisShop> selectByExampleWithBLOBs(BisShopExample example) {
		return null;
	}

	@Override
	public BisShop selectByPrimaryKey(String shopId) {
		return bisShopMapper.selectByPrimaryKey(shopId);
	}

	

	

	
	@Override
	public List<BisShop> selectByExample(Map<String, Object> map) {
		String typeName=(String)map.get("typeName");
		System.out.println(typeName);
		BisShopExample example=new BisShopExample();
		Criteria createCriteria = example.createCriteria();
		
		if (StringUtils.isNotBlank(typeName)) {
			createCriteria.andShopProjectLike("%"+typeName+"%");
		}
		createCriteria.andShowStatusEqualTo("1");
		return this.bisShopMapper.selectByExample(example);
	}

	@Override
	public BisCustomUser queryUserBeautyNum(String userId) {
		return bisCustomUserMapper.selectByPrimaryKey(userId);
	}
    
	@Transactional
	@Override
	public Map<String, Object> shopBeauty(Map<String, Object> map) {
		
		String projectId=(String) map.get("projectId");
		String shopId=(String) map.get("shopId");
		BisCustomUser bisCustomUser=(BisCustomUser) map.get("bisCustomUser");
		String subscribeTime=(String) map.get("subscribeTime");
		String str=map.get("subscribeDeposit").toString();
		str=str.substring(1);
		double subscribeDeposit=  Double.parseDouble(str) ;
		BisNurseProject bisNurseProject=bisNurseProjectMapper.selectByPrimaryKey(projectId);
		
		String customUserId=bisCustomUser.getCustomUserId();
		
		Snowflake snowflake=new Snowflake(0, 0);
		String payId=snowflake.nextId()+"";
		String orderId=snowflake.nextId()+"";
		BisOrderPay bisOrderPay=new BisOrderPay();
		bisOrderPay.setPayId(payId);
		bisOrderPay.setOrderId(orderId);
		bisOrderPay.setBuyAccount(bisCustomUser.getOpenid());
		bisOrderPay.setPayCode("A"+payId);
		bisOrderPay.setPayWay("1");
		bisOrderPay.setPayStatus("1");
		
		
		bisOrderPay.setCreateTime(new Date());
		bisOrderPay.setPayMoney(subscribeDeposit);
		bisOrderPay.setPayType("1");
		bisOrderPay.setPayBack("2");
		bisOrderPayMapper.insert(bisOrderPay);
		BisOrderDeposit depositPO = new BisOrderDeposit(); // 定金支付信息
		depositPO.setDepositId(payId);
		depositPO.setDepositMoney(subscribeDeposit);
		depositPO.setOrderId(orderId);
		depositPO.setPayWay("3");
		depositPO.setCustomUserId(customUserId);
		depositPO.setDepositStatus("1");
		bisOrderDeposit.insert(depositPO);
		
		
		BisBusinessOrder orderPO = new BisBusinessOrder();
		orderPO.setOrderId(orderId);
		orderPO.setProjectId(projectId);
		orderPO.setCustomUserId(customUserId);
		orderPO.setOrderContent(bisNurseProject.getProjectName());
		orderPO.setOrderMoney(bisNurseProject.getRmbPrice());
		orderPO.setShopId(shopId);
		orderPO.setShopId(shopId);
		orderPO.setSubscribeTime(DateUtils.getYmdDate(subscribeTime)); //暂时的
		orderPO.setCreateTime(new Date());
		orderPO.setOrderStatus("1");
		orderPO.setOrderSource("2");
		orderPO.setOrderType("1");
		orderPO.setPayWay("3");
		orderPO.setBuyNum(1);

		BisShopCustomExample example=new BisShopCustomExample();
		com.system.common.pojo.BisShopCustomExample.Criteria criteria=example.createCriteria();
		criteria.andShopIdEqualTo(shopId);
		criteria.andCustomUserIdEqualTo(customUserId);
		List<BisShopCustom> shopCustomList=shopCustomMapper.selectByExample(example);
		BisShopCustom bisShopCustom=new BisShopCustom();
		bisShopCustom.setCustomUserId(bisCustomUser.getCustomUserId());
		bisShopCustom.setShopId(shopId);
		bisShopCustom.setRecentTime(new Date());
		if (shopCustomList.size()<0) {
			bisShopCustom.setCreateTime(new Date());
			shopCustomMapper.insert(bisShopCustom);
		}else{
			shopCustomMapper.updateByPrimaryKey(bisShopCustom);
		}
		
		bisBusinessOrderMapper.insert(orderPO);
		double beauty=bisCustomUser.getBeautyNum();
		Map<String, Object> map2=new HashMap<>();
		if (subscribeDeposit>beauty) {
			map2.put("msg", "美颜币不足！！");
			return map2;
		}
		 beauty=beauty-subscribeDeposit;
		bisCustomUser.setBeautyNum((int)beauty);
		int state=bisCustomUserMapper.updateByPrimaryKey(bisCustomUser);
		
		if (state>0) {
			
			
			depositPO.setDepositStatus("2");
			bisOrderDeposit.updateByPrimaryKey(depositPO);
			
			map2.put("state", "1");
		}else {
			map2.put("msg", "支付失败");
		}
		return map2;
	}

	@Override
	public List<BisBeautyConfig> queryBeautyConfig() {
		BisBeautyConfigExample example=new BisBeautyConfigExample();
		com.system.common.pojo.BisBeautyConfigExample.Criteria criteria=example.createCriteria();
		criteria.andIsDelEqualTo("0");
		example.setOrderByClause("buy_num ASC");
		List<BisBeautyConfig> bbgList=this.bisBeautyConfigMapper.selectByExample(example);
		
		
		return bbgList;
	}

	@Override
	public Map<String, Object> saveBuyBeautyOrder(HttpServletRequest request, BisCustomUser bisCustomUser) {
		Map<String, Object> map=new HashMap<>();
		String shopId=request.getParameter("shop_id");
		String configId=request.getParameter("config_id");
		BisBeautyConfig bisBeautyConfig=bisBeautyConfigMapper.selectByPrimaryKey(configId);
		int buy_num=bisBeautyConfig.getBuyNum();
		int give_num=bisBeautyConfig.getGiveNum();
		int total_price=buy_num+give_num;
		if (total_price<0) {
			map.put("msg", "充值不能为0");
			return map;
		}
		String order_content = "购买颜值" + buy_num + "个(赠送" + give_num + "个)";
		String openid = bisCustomUser.getOpenid();
		String custom_user_id = bisCustomUser.getCustomUserId();
		Snowflake snowflake=new Snowflake(0, 0);
		String order_id=snowflake.nextId()+"";
		String payId=snowflake.nextId()+"";
		// 保存支付记录信息
		BisOrderPay orderPayPO = new BisOrderPay();
		
		orderPayPO.setPayId(payId);
		orderPayPO.setOrderId(order_id);
		orderPayPO.setBuyAccount(openid);
		orderPayPO.setPayCode("M"+payId);
		orderPayPO.setPayWay("1");
		orderPayPO.setPayStatus("1");
		orderPayPO.setCreateTime(new Date());
		orderPayPO.setPayMoney((double)total_price);
		orderPayPO.setPayType("1");
		orderPayPO.setPayBack("2");
		orderPayPO.setBuyAccount(openid);
		bisOrderPayMapper.insert(orderPayPO);

		// 保存订单信息
		BisBusinessOrder orderPO = new BisBusinessOrder();
		orderPO.setOrderId(order_id);
		orderPO.setProjectId(configId);
		orderPO.setCustomUserId(custom_user_id);
		orderPO.setOrderContent(order_content);
		orderPO.setOrderMoney((double)total_price);
		orderPO.setCreateTime(new Date());
		orderPO.setOrderStatus("0");
		orderPO.setOrderSource("2");
		orderPO.setOrderType("2");
		orderPO.setBuyNum(buy_num);
		orderPO.setShopId(shopId);
		int row = bisBusinessOrderMapper.insert(orderPO);
		
		
		if (row > 0) {
			map.put("state","1");
			int beautyNum=bisCustomUser.getBeautyNum();
			beautyNum+=total_price;
			bisCustomUser.setBeautyNum(beautyNum);
			bisCustomUserMapper.updateByPrimaryKey(bisCustomUser);
			
			request.getSession().setAttribute("user", bisCustomUser);
			BisBeautyRecord bisBeautyRecord=new BisBeautyRecord();
			String recordId=snowflake.nextId()+"";
			bisBeautyRecord.setRecordId(recordId);
			bisBeautyRecord.setOrderId(order_id);
			bisBeautyRecord.setCustomUserId(bisCustomUser.getCustomUserId());
			bisBeautyRecord.setBeautyNum(buy_num+give_num);
			bisBeautyRecord.setRecordType("1");
			bisBeautyRecord.setPayTime(new Date());
			bisBeautyRecord.setVaildStatus("1");
			bisBeautyRecordMapper.insert(bisBeautyRecord);
			
			
		} else {
			map.put("msg","操作完成，购买颜值失败，颜值订单无法保存成功。");
			
		}
		return map;
	}

	@Override
	public List<BisBeautyRecord> queryBeautyRecord() {
		
		BisBeautyRecordExample example=new BisBeautyRecordExample();
		com.system.common.pojo.BisBeautyRecordExample.Criteria criteria=example.createCriteria();
		criteria.andVaildStatusEqualTo("1");
		return this.bisBeautyRecordMapper.selectByExample(example);
	}

	@Override
	public List<BisShop> ShopCollect(String userId) {
		BisShopCollectExample example=new BisShopCollectExample();
		com.system.common.pojo.BisShopCollectExample.Criteria criteria=example.createCriteria();
		criteria.andCustomUserIdEqualTo(userId);
		List<BisShopCollect> bisShopCollects=this.bisShopCollectMapper.selectByExample(example);
		List<BisShop> shopList=new ArrayList<>();
		
		if (bisShopCollects.size()>0) {
			for (BisShopCollect bisShopCollect : bisShopCollects) {
				shopList.add(this.bisShopMapper.selectByPrimaryKey(bisShopCollect.getShopId()));
				
			}
			return shopList;
		}
		
		return null;
	}

	@Override
	public List<Map<String, Object>> orderProject(BisCustomUser bisCustomUser) {
		List<Map<String, Object>> map=bisBusinessOrderMapper.queryByUserId(bisCustomUser.getCustomUserId());
		
		return map;
	}
	
	@Override
	public Map<String,Object> deleteOderId(String order_id){
		Map<String,Object> map=new HashMap<>();
		int a=bisBusinessOrderMapper.deleteByPrimaryKey(order_id);
		System.out.print(a);
		if (a>0){
			map.put("appcode","1");
			map.put("appmsg","成功取消订单！");
		}else {
			map.put("appcode","2");
			map.put("appmsg","取消订单失败！");
		}
		return  map;
	}

	@Override
	@Transactional
	public Map<String, Object> payOrder(BisCustomUser user, HttpServletRequest request) {
		Map<String,Object> map=new HashMap<>();
        String order_id=request.getParameter("order_id");
        BisBusinessOrder bisBusinessOrder=bisBusinessOrderMapper.selectByPrimaryKey(order_id);
        bisBusinessOrder.setOrderStatus("4");
		bisBusinessOrder.setCustomUserId(user.getCustomUserId());
        int state=bisBusinessOrderMapper.updateByPrimaryKey(bisBusinessOrder);
		BisOrderPay orderPayPO = new BisOrderPay();
		Snowflake snowflake=new Snowflake(0, 0);
		String payId=snowflake.nextId()+"";
		String orderId=snowflake.nextId()+"";
		orderPayPO.setPayId(payId);
		orderPayPO.setOrderId(orderId);

		orderPayPO.setPayCode("M"+payId);
		orderPayPO.setPayWay("1");
		orderPayPO.setPayStatus("1");
		orderPayPO.setCreateTime(new Date());
		double payNoney=bisBusinessOrder.getOrderMoney();
		orderPayPO.setPayMoney(payNoney);
		orderPayPO.setPayType("1");
		orderPayPO.setPayBack("2");

		bisOrderPayMapper.insert(orderPayPO);
		if (state>0){
			map.put("state","1");
		}
		double beauyNum=user.getBeautyNum()-payNoney;
		user.setBeautyNum((int)beauyNum);
		bisCustomUserMapper.updateByPrimaryKey(user);
		HttpSession session=request.getSession();
		session.setAttribute("user",user);
		return map;
	}


}
